<?php
/**
 * Created by PhpStorm.
 * User: zws
 * Date: 2018/9/8
 * Time: 11:02
 * 充电桩管理
 */
namespace app\index\service;

use app\common\model\ChargingModel;
use think\Exception;

class ChargingService
{
    protected static $chargingModel;
    /**
     * UserService constructor.
     * @param ChargingModel $chargingModel
     * @author zws
     */
    public function __construct(ChargingModel $chargingModel)
    {
        self::$chargingModel = $chargingModel;
    }

    /**
     * 添加充电桩
     * @param $param
     * @return mixed
     * @author zws
     */
    public function add($param)
    {
        if (empty($param) || !isset($param['charge_num'])) {
            app_fail(9997);
        }
        if(empty($param['charge_num']))
        {
            app_fail(8998);
        }
        if(!isset($param['rated_voltage']) || empty($param['rated_voltage']))
        {
            app_fail(8997);
        }
        if(!isset($param['rated_current']) || empty($param['rated_current']))
        {
            app_fail(8996);
        }
        if(!isset($param['charge_type']) || empty($param['charge_type']))
        {
            app_fail(8996);
        }
        // 检查充电桩是否存在
        $chargeModel = self::$chargingModel;
        $row = $chargeModel->where('charge_num', '=', $param['charge_num'])->find();
        if (!empty($row)) {
            app_fail(8995);
        }
        try {
            $chargeModel->allowField(true)->save($param);
            return ['charge_id'=>$chargeModel->id];
        } catch (\Exception $e) {
            app_fail(9998);
        }
    }

    /**
     * 查询充电桩
     * @param $param
     * @return array
     * @author zws
     */
    public function chargeInfo($param)
    {
        $param['page'] = isset($param['page']) ? isset($param['page']) : 1;
        $param['limit'] = isset($param['limit']) ? isset($param['limit']) : 10;
        $map = [];
        $map[] = ['is_del', "=", 1];
        if(isset($param['id']))
        {
            $map[] = ['id', '=', $param['id']];
        }
        if(isset($param['charge_num']))
        {
            $map[] = ['charge_num', 'like', '%'.$param['charge_num']].'%';
        }
        // 获取总数
        $count = self::$chargingModel->where($map)->count();
        if ($count < 1) {
            return ['data' => [], 'count' => $count];
        }
        $result = self::$chargingModel->where($map)
            ->page($param['page'])
            ->limit($param['limit'])
            ->order('create_time', 'desc')
            ->select();
        $dataArr = [];
        if($result)
        {
            $charge_type = config("param_config.charge_type");
            $charge_status = config("param_config.charge_status");

            foreach ($result as $k=>$v)
            {
                $dataArr[$k] = array(
                    'id'=>$v->id,
                    'charge_num'=>$v->charge_num,
                    'charge_name'=>$v->charge_name,
                    'charge_type'=>isset($charge_type[$v->charge_type]) ?
                        $charge_type[$v->charge_type] : '',
                    'address'=>$v->address,
                    'rated_voltage'=>$v->rated_voltage,
                    'rated_current'=>$v->rated_current,
                    'create_time'=>$v->create_time,
                    'charge_status'=>isset($charge_status[$v->status]) ?
                        $charge_status[$v->status] : '',
                );
            }
        }
        return ['data' => $dataArr, 'count' => $count];
    }
}